/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package opensocial.apps.common;

import java.net.URL;

import opensocial.apps.common.config.ConfigFactory;

import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.config.ConfigParser;
import org.apache.log4j.Logger;

/**
 * 
 * <p>
 * Sample App Catalog file.
 * 
 * <pre>
 * &lt;catalogs&gt;
 *     &lt;catalog name=&quot;xiaonei&quot;&gt;
 *         &lt;chain name=&quot;ProfileCheck&quot;&gt;
 *             &lt;command id=&quot;command1&quot; class=&quot;&quot;&gt;
 *             &lt;command id=&quot;command2&quot; class=&quot;&quot;&gt;
 *         &lt;/chain&gt;
 *         &lt;command name=&quot;command1&quot; class=&quot;&quot;&gt;
 *     &lt;/catalog&gt;
 *     &lt;catalog name=&quot;51&quot;&gt;
 *     &lt;/catalog&gt;
 *     &lt;catalog name=&quot;default&quot;&gt;
 *     &lt;/catalog&gt;
 * &lt;/catalogs&gt;
 * </pre>
 * 
 * @author Sirius
 */
public class CatalogFactory {

  private static final Logger LOGGER = Logger.getLogger(CatalogFactory.class);

  private static final String CONFIG_FILE_PROPERTY_NAME_DEFAULT_PREFIX = "config/";
  private static final String CONFIG_FILE_PROPERTY_NAME_DEFAULT = "catalog.xml";
  private static org.apache.commons.chain.CatalogFactory cfb = org.apache.commons.chain.CatalogFactory
      .getInstance();

  private static CatalogFactory _instance;

  public static synchronized CatalogFactory getCatalogFactory()
      throws Exception {

    if (_instance == null) {
      try {
        _instance = new CatalogFactory();
      } catch (Exception e) {
        LOGGER.error("Failed to load catalog for app:" + e.getMessage());
      }
    }

    return _instance;
  }

  public Catalog getCatalog(String ctn) throws Exception {

    if (!ConfigFactory.isValidContainerId(ctn)) {
      LOGGER.error("Failed to load catalog for container '" + ctn
          + "': container id is invalid.");
      return null;

    }

    Catalog catalog = cfb.getCatalog(ctn);
    if (catalog == null) {
      catalog = cfb.getCatalog("default");
      LOGGER.warn("Failed to load catalog for app."
          + ":default catalog is loaded.");
    }

    return catalog;
  }

  private CatalogFactory() throws Exception {

    ConfigParser parser = new ConfigParser();

    URL url = this.getClass().getResource(
        "/" + CONFIG_FILE_PROPERTY_NAME_DEFAULT_PREFIX
            + CONFIG_FILE_PROPERTY_NAME_DEFAULT);
    parser.parse(url);

  }
}
